package com.surelution.projects.congress

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import org.codehaus.groovy.grails.commons.ApplicationHolder;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGDecodeParam;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

class MemberController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [memberInstanceList: Member.list(params), memberInstanceTotal: Member.count()]
    }

    def create = {
        def memberInstance = new Member()
        memberInstance.properties = params
        return [memberInstance: memberInstance]
    }

    def save = {
		def upload = params.picture
		
//		def inputStream = upload.inputStream
//		JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(inputStream);
//		JPEGDecodeParam param = decoder.getJPEGDecodeParam();
//		println "width:" + param.width
//		println "height:" + param.height
//		println "x:" + param.xDensity
//		println "y:" + param.yDensity
		
		
        def memberInstance = new Member(params)
        if (memberInstance.save(flush: true)) {
			if(upload) {
//				BufferedImage bi = ImageIO.read(upload.inputStream)
//				println bi.width
//				println bi.height
				def path = grailsAttributes.applicationContext.getResource("pics/").file.toString()
				def saveTo = new File(path + "/" + memberInstance.id + ".jpg")
				upload.transferTo(saveTo)
				
			}
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'member.label', default: 'Member'), memberInstance.id])}"
            redirect(action: "show", id: memberInstance.id)
        }
        else {
            render(view: "create", model: [memberInstance: memberInstance])
        }
    }

    def show = {
        def memberInstance = Member.get(params.id)
        if (!memberInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'member.label', default: 'Member'), params.id])}"
            redirect(action: "list")
        }
        else {
            [memberInstance: memberInstance]
        }
    }

    def edit = {
        def memberInstance = Member.get(params.id)
        if (!memberInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'member.label', default: 'Member'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [memberInstance: memberInstance]
        }
    }

    def update = {
        def memberInstance = Member.get(params.id)
        if (memberInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (memberInstance.version > version) {
                    
                    memberInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'member.label', default: 'Member')] as Object[], "Another user has updated this Member while you were editing")
                    render(view: "edit", model: [memberInstance: memberInstance])
                    return
                }
            }
            memberInstance.properties = params
            if (!memberInstance.hasErrors() && memberInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'member.label', default: 'Member'), memberInstance.id])}"
                redirect(action: "show", id: memberInstance.id)
            }
            else {
                render(view: "edit", model: [memberInstance: memberInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'member.label', default: 'Member'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def memberInstance = Member.get(params.id)
        if (memberInstance) {
            try {
                memberInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'member.label', default: 'Member'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'member.label', default: 'Member'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'member.label', default: 'Member'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def suspendShow = {
        def memberInstance = Member.get(params.id)
		[memberInstance:memberInstance]
	}
	
	def suspend = {
		def endDate = params.endDate
		
        def memberInstance = Member.get(params.id)
		if(memberInstance) {
			if(!endDate) {
				endDate = new Date()
			}
			memberInstance.endDate = endDate
			memberInstance.ended = true
			memberInstance.save(flush:true)
		}
		redirect(action:"show", id:params.id)
	}
}
